package com.shop.util;

import java.util.HashMap;
import java.util.Map;

import com.JSON2.json.JSONObject;
import com.shop.entity.MerchantOrder;
import com.shop.entity.OrderGoodsList;

/**
 * 
 * <b>结算之商品按商户id分类下单<b>
 * @author 威 
 * <br>2018年3月14日 下午7:09:14 
 *
 */
public class SettDetailA {
	/**
	 * 商户id为键 商户订单实体类为value
	 */
	private Map<Integer, MerchantOrder> maps ;
	/**
	 * 
	 * 创建
	 *
	 */
	public SettDetailA(Integer merchantId, JSONObject object){
		System.out.println("创建SettDetailA") ;
		maps = new HashMap<Integer, MerchantOrder>() ;
		create(merchantId, object) ;
	}
	/**
	 * 
	 * 比较的入口
	 * @see
	 * @param mchantId
	 * @param object
	 * @return
	 * Boolean
	 *
	 */
	public Boolean startDo(Integer mchantId, JSONObject object){
		System.out.println("startDo-" + mchantId);
		Boolean isHave = false ;
		for(Integer key : maps.keySet()){
			if(key == mchantId){
				System.out.println("遍历key的结果： " + key) ;
				relation(key, object) ; //存在关联的商户订单
				isHave = true ;
				break ;
			}
		}
		if(!isHave) create(mchantId, object) ;  //不存在商户订单
		return true ;
	}
	/**
	 * 
	 * 比较结束 
	 * @see
	 * @return
	 * Map<Integer,MerchantOrder>
	 *
	 */
	public Map<Integer, MerchantOrder> end(){
		return maps ;
	}
	/**
	 * 
	 * 存在关联的商户订单
	 * 
	 * @see
	 * @param mchantId
	 * @param object
	 * void
	 *
	 */
	private void relation(Integer mchantId, JSONObject object){
		MerchantOrder mOrder = maps.get(mchantId) ;
		mOrder.setTotalAmount(mOrder.getTotalAmount() + ((Integer) object.get("price")));
		OrderGoodsList oGoods = createOrderGoods(object) ;
		System.out.println("关联id：" + mchantId) ;
		mOrder.getOrderGoodsLists().add(oGoods) ;
		
		maps.put(mchantId, mOrder) ; //保持最新的订单
		System.out.println("将"+mchantId+"更新放入maps") ;
	}
	/**
	 * 
	 * 不存在商户订单 
	 * 需要新建该商户订单并加入maps中
	 * @see
	 * @param mchantId
	 * @param object
	 * void
	 *
	 */
	private void create(Integer mchantId, JSONObject object){
		System.out.println("无关联id：" + mchantId);
		MerchantOrder mOrder = creatMOrder() ; //创建mOrder
		System.out.println(object.get("price")) ;
		mOrder.setTotalAmount(Double.valueOf((Integer) object.get("price"))) ;
		OrderGoodsList oGoods = createOrderGoods(object) ;
		mOrder.getOrderGoodsLists().add(oGoods) ;
		maps.put(mchantId, mOrder) ;
		System.out.println("将"+mchantId+"放入maps") ;
	}	
	/**
	 * 
	 * 创建一个商户订单 
	 * 用order关联他
	 * @see
	 * @return
	 * MerchantOrder
	 *
	 */
	private MerchantOrder creatMOrder() {
		MerchantOrder mOrder = new MerchantOrder() ;
		mOrder.setDeliveryState(false) ;
		mOrder.setProgressState(false) ;
		return mOrder ;
	}
	/**
	 * 
	 * 创建一个订单商品项
	 * @see
	 * @param object
	 * @return
	 * OrderGoodsList
	 *
	 */
	private OrderGoodsList createOrderGoods(JSONObject object){
		OrderGoodsList oGoods = new OrderGoodsList() ;
		oGoods.setColor((String) object.get("color")) ;
		oGoods.setDiscount(1.0) ;
		oGoods.setGoods_id((Integer) object.get("goodsId")) ;
		oGoods.setQuantity((Integer) object.get("count")) ;
		oGoods.setSize((String) object.get("size")) ;
		return oGoods ;
	}
}